<template>
  <div>
    <a-modal title="添加/编辑分类" :visible="createVisible" centered @ok="submitCreate" @cancel="createVisible = false"
      width="406px">
      <a-form layout="inline" class="drawer-form">
        <a-form-item label=" 请输入分类名称">
          <a-input v-model="content" label="分类名称" placeholder="请输入" />
        </a-form-item>
        <br />
        <a-form-item label="上一级分类" v-if="typeInfo != ''">
          {{ typeInfo }}
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>
<script>
import { sourceChange, sourceCreate, sourceSuperior } from '@/api/dcc'
export default {
  data() {
    return {
      content: '',
      createVisible: false,
      id: "",
      typeInfo: "",
      jobs: ""
    };
  },
  methods: {
    open() {
      this.createVisible = true;
      this.content = ""
      this.jobs = "create"
    },
    async insert(source) {
      this.typeInfo = ""
      this.content = ""
      this.createVisible = true;
      this.superior = source.SourceSuperior
      this.id = source.Id
      this.jobs = "insert"

      const { data } = await sourceSuperior({ id: source.Id })
      try {
        if (data[0].value != "") {
          this.typeInfo = data[0].value + "->" + source.SourceName
        }
      } catch (error) {
        this.$message.error(error)
      }
    },
    edit(data) {
      this.id = data.Id
      this.content = data.SourceName
      this.superior = data.SourceSuperior
      this.createVisible = true;
      this.jobs = "change"
    },
    async submitCreate() {
      if (this.jobs == "create") {
        try {
          await sourceCreate({ SourceName: this.content, SourceSuperior: 0 })
          this.createVisible = false;
          this.$emit('succeed')
          this.$message.success('操作分类成功');
        } catch (error) {
          this.$message.error(error)
        } finally {
        }
      } else if (this.jobs == "insert") {
        try {
          await sourceCreate({ SourceName: this.content, SourceSuperior: this.id })
          this.createVisible = false;
          this.$emit('succeed')
          this.$message.success('操作分类成功');
        } catch (error) {
          this.$message.error(error)
        } finally {
        }
      } else {
        try {
          await sourceChange({ SourceName: this.content, id: this.id, SourceSuperior: this.superior })
          this.createVisible = false;
          this.$emit('succeed')
          this.$message.success('操作分类成功');
        } catch (error) {
          this.$message.error(error)
        } finally {
        }
      }
    }
  },
};
</script>
<style lang="less" scoped></style>